﻿using Newtonsoft.Json;
using System.Threading.Tasks;

namespace Ssdb.Provider
{
    /// <summary>
    ///     Class SsdbClient.
    /// </summary>
    public class SsdbClient
        : BaseSsdbClient
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="BaseSsdbClient" /> class.
        /// </summary>
        /// <param name="hostName">Name of the host.</param>
        /// <param name="port">The port.</param>
        private SsdbClient(string hostName, int port)
            : base(hostName, port)
        {
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="BaseSsdbClient" /> class.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        private SsdbClient(string connectionString = "SsdbConnectiongString")
            : base(connectionString)
        {
        }

        /// <summary>
        ///     Adds the specified key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public bool Add<T>(string key, T value)
        {
            return this.Set(key, JsonConvert.SerializeObject(value));
        }

        /// <summary>
        ///     Gets the specified key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <returns>T.</returns>
        public T Get<T>(string key)
        {
            string val;
            var result = this.Get(key, out val);
            if (result && !string.IsNullOrEmpty(val))
            {
                return JsonConvert.DeserializeObject<T>(val);
            }
            return default(T);
        }

        /// <summary>
        ///     Adds the asynchronous.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <returns>Task&lt;System.Boolean&gt;.</returns>
        public Task<bool> AddAsync<T>(string key, T value)
        {
            return Task.FromResult(false);
        }

        #region Instance

        /// <summary>
        ///     Gets the client.
        /// </summary>
        /// <returns>SsdbClient.</returns>
        public static SsdbClient GetClient()
        {
            return new SsdbClient();
        }

        /// <summary>
        ///     Gets the client.
        /// </summary>
        /// <param name="hostName">Name of the host.</param>
        /// <param name="port">The port.</param>
        /// <returns>SsdbClient.</returns>
        public static SsdbClient GetClient(string hostName, int port)
        {
            return new SsdbClient(hostName, port);
        }

        // ReSharper disable once MethodOverloadWithOptionalParameter
        /// <summary>
        ///     Gets the client.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <returns>SsdbClient.</returns>
        public static SsdbClient GetClient(string connectionString = "SsdbConnectiongString")
        {
            return new SsdbClient(connectionString);
        }

        #endregion Instance
    }
}